<?php


namespace app\appapi\controller;


use app\common\model\GoodsModel;
use app\common\model\OrderReceiptModel;
use app\common\model\StorageLogModel;
use app\common\model\StorageModel;
use cmf\controller\RestBaseController;
use think\db\Query;
use think\Request;

class GoodsController extends RestBaseController
{
    public function search_goods(Request $request)
    {
        $param = $request->param();
        $data = GoodsModel::where(function (Query $query) use ($param) {
            $query->where('isdel', 0);
            if (!empty($param['class_id'])) {
                $query->where('cid', $param['class_id']);
            }
            if (!empty($param['storage_id'])) {
                $goodsIds = StorageLogModel::where('sid', $param['storage_id'])->where('goods_num', '>', 0)->column('goods_id');
                $query->where('id', 'in', $goodsIds);
            }
            if (!empty($param['keywords'])) {
                $query->where('goods_name|goods_subname', 'like', "%{$param['keywords']}%");
            }
            if (!empty($param['goods_type'])) {
                switch ($param['goods_type']) {
                    case 1:
                        $query->order('goods_price');
                        break;
                    case 2:
                        $query->order('goods_sale_num', 'desc');
                        break;
                    case 3:
                        $query->order('weight', 'desc');
                        break;
                    case 4:
                        $query->order('create_time', 'desc');
                        break;
                    default:
                        $query->order('create_time desc, goods_sale_num desc');
                        break;
                }
            }
        })->paginate()->each(function ($v, $k) {
            $v['goods_storage'] = StorageLogModel::where('goods_id', $v['id'])->select()
                ->each(function ($vv, $kk) {
                    $vv['freight_price'] = StorageModel::where('id', $vv['sid'])->value('price');
                });
            $v['image'] = cmf_get_image_url($v['image']);
            return $v;
        });
        $class_list = GoodsModel::getClass();

        $storage_list = StorageModel::where('is_del', 0)->order('id desc')->select()->toArray();
        $prefix = [
            'id'           => 0,
            'mid'          => 0,
            'sid'          => 0,
            'store_name'   => '全部仓储',
            'weight'       => 0.00,
            'mark'         => '',
            'price'        => '0.00',
            'is_del'       => 0,
            'send_address' => '',
            'plattype'     => '',
        ];
        array_unshift($storage_list, $prefix);
        $this->success('ok', compact('data', 'class_list', 'storage_list'));
    }

    /**
     * @param string $goods_id
     *
     * add_time: 1585740625
     * bid: 2
     * chose_attr: ""
     * chose_spec_id: ""
     * cid: 1
     * edit_time: 1590936665
     * free_freight: "0.00"
     * freight_id: 0
     * goods_collect: 0
     * goods_content: "<p style="text-align: center;"><img src="http://daifatu.oss-cn-hangzhou.aliyuncs.com/shop/editor/2020_04_01/15857406153735.jpg" style="max-width:100%;"><img src="http://daifatu.oss-cn-hangzhou.aliyuncs.com/shop/editor/2020_04_01/15857406228049.jpg" style="max-width: 100%;"><br></p>"
     * goods_freight: "0.00"
     * goods_images: ["http://daifatu.oss-cn-hangzhou.aliyuncs.com/shop/goods/2020_04_01/15857405475595.jpg"]
     * goods_images_thumb: ["http://daifatu.oss-cn-hangzhou.aliyuncs.com/shop/goods/2020_04_01/15857405475595_400.jpg"]
     * goods_images_thumb_200: ["http://daifatu.oss-cn-hangzhou.aliyuncs.com/shop/goods/2020_04_01/15857405475595_200.jpg"]
     * goods_market_price: "0.03"
     * goods_master_image: "http://daifatu.oss-cn-hangzhou.aliyuncs.com/shop/goods/2020_04_01/15857405475595.jpg"
     * goods_name: "星座香包"
     * goods_no: "XZXB"
     * goods_num: 70199
     * goods_price: "0.10"
     * goods_sale_num: 65132
     * goods_status: 1
     * goods_storage: [{id: 31, sid: 1, storage_name: "义乌仓储", goods_id: 15, goods_num: 70220},…]
     *  0: {id: 31, sid: 1, storage_name: "义乌仓储", goods_id: 15, goods_num: 70220}
     *  1: {id: 65, sid: 5, storage_name: "广州（中通）仓", goods_id: 15, goods_num: 0}
     * goods_subname: "香薰包，星座香袋，除味居家必备[1个]多种香味随机发"
     * goods_verify: 1
     * groupbuy_num: 5
     * groupbuy_price: "0.00"
     * id: 15
     * image: "http://daifatu.oss-cn-hangzhou.aliyuncs.com/shop/goods/2020_04_01/15857405475595_200.jpg"
     * is_groupbuy: 0
     * is_index: 0
     * is_new: 0
     * is_top: 0
     * store_goods_class: 0
     * user_id: 0
     * warning_num: 2000
     * weight: "0.01"
     */

    public function get_goods_info($goods_id = '')
    {
        $data = GoodsModel::find($goods_id);
        $data['goods_storage'] = StorageLogModel::where('goods_id', $goods_id)->select()
            ->each(function ($vv, $kk) {
                $vv['freight_price'] = StorageModel::where('id', $vv['sid'])->value('price');
            });
        if ($data['goods_images']) {
            $goods_images = json_decode($data['goods_images'], true);
            foreach ($goods_images as $k => $val) {
                $goods_images[$k] = cmf_get_image_url($val);
            }
            $data['goods_images'] = $goods_images;
        } else {
            $data['goods_images'] = [];
        }
        $this->success('ok', $data);
    }

    /**
     * 私有cang
     */
    public function search_purchase_goods($p, $cid, $sid, $goods_tp, $order_tp)
    {
        $this->success('ok', []);
    }

    /**
     * 历史购买
     */
    public function get_history_goods()
    {
        $goodsIds = OrderReceiptModel::where('user_id', $this->userId)->column('goods_id');
        $data = GoodsModel::where('id', 'in', $goodsIds)->where('isdel', 0)->order('id desc')->paginate();
        $this->success('ok', $data);
    }
}